﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using VoteSystem.Models;
using VoteSystem.Services;
using VoteSystem.Filters;

namespace VoteSystem.Controllers
{
    public class AccountController : Controller
    {

        public VoteService voteService = new VoteService();
        AccountService accountService = new AccountService();
       
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {

            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            var result = accountService.Login(model);
            return Json(result, JsonRequestBehavior.DenyGet);
        }

        [SessionFilter]
        public ActionResult ShowVoteList()
        {
            var currentUser = (UserInSession)Session["loginUser"];
            var list = voteService.QueryVoteViewModel(currentUser.DeptName);
            var config = voteService.GetCurrentConfig();
            ViewBag.Config = config;
            return View(list);
        }

        /// <summary>
        /// 按志愿批次查询
        /// </summary>
        /// <param name="turn"></param>
        /// <returns></returns>
        public ActionResult SperateShowVoteList(int round)
        {
            var currentUser = (UserInSession)Session["loginUser"];
            var list = voteService.QueryVoteViewModel(currentUser.DeptName);
            var config = voteService.GetCurrentConfig();
            ViewBag.Config = config;
            ViewBag.Round = round;
            return View(list);
        }


        public ActionResult LogOff()
        {
            if (Session["loginUser"] != null) 
            {
                accountService.LoginOff(((UserInSession)Session["loginUser"]).UserName);
                Session.Abandon();
            }
            return RedirectToAction("Login", "Account");
        }

        public ActionResult Decide(int positionId, int employeeId,int round)
        {
            var result = voteService.Decide(positionId, employeeId, round);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UnDecide(int positionId, int employeeId, int round) 
        {
            var result = voteService.UnDecide(positionId, employeeId, round);
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [SessionFilter]
        public ActionResult ChangePassword()
        { 
           return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(string oldPassword, string newPassword)
        {
            return Json(accountService.ChangePassword(oldPassword,newPassword));
        }
    }
}
